<link href="__PUBLIC__/css/style.default.css" rel="stylesheet">

<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="__PUBLIC__/js/html5shiv.js"></script>
<script src="__PUBLIC__/js/respond.min.js"></script>
<![endif]-->

<div class="mainpanel">
    <div class="pageheader">
        <div class="media">
            <div class="pageicon pull-left">
                <i class="fa fa-th-list"></i>
            </div>
            <div class="media-body">
                <ul class="breadcrumb">
                    <li><a href=""><i class="glyphicon glyphicon-home"></i></a></li>
                    <li><a href="">首页</a></li>
                    <li>机构管理</li>
                </ul>
                <h4>机构管理</h4>
            </div>
        </div><!-- media -->
    </div>

    <div class="contentpanel">

        <div class="panel panel-primary-head">
            <div id="succ_file_s" class="alert alert-success hides">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                <strong>机构添加成功</strong>
            </div>

            <div id="faild_file_s" class="alert alert-danger hides">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                <strong>机构添加失败,请检查！</strong>
            </div>

            <div class="row" style="padding: 0 15px">
                <!--<button id="USER_MADOL_ADD_BTN" class="btn btn-primary" >添加用户</button>-->


            </div>
            <div class="row">
                <div class="col-md-6">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <div class="panel-btns" style="display: none;">
                                <a href="" class="panel-minimize tooltips maximize" data-toggle="tooltip" title="" data-original-title="Minimize Panel">
                                    <i class="fa fa-plus"></i>
                                </a>
                            </div><!-- panel-btns -->
                            <h4 class="panel-title">添加机构</h4>
                            <p style="    color: #fff;">点击右上角显示更多</p>
                        </div><!-- panel-heading -->
                        <div class="panel-body" style="display: none;">
                            <form id="user_add_ajaxForm" method="post" action="{:url('admin/org/orgadd')}" enctype="multipart/form-data">

                                <div class="row">
                                    <div class="form-group col-md-4">
                                        <label>机构名称</label>
                                        <input type="text" name="org_name" placeholder="机构名称" class="form-control">
                                    </div>
                                    <div class="form-group col-md-4 hides">
                                        <label>机构类别</label>

                                        <select name="org_grade" class="form-control">
                                            {volist name="emsconfig['orggradeconfig']" id="vo" key="k"}
                                            <option value="{$k}">{$vo}</option>
                                            {/volist}
                                        </select>
                                    </div>
                                    <div class="form-group col-md-4">
                                        <label>邮编</label>
                                        <input type="text" name="post_code" placeholder="邮编" class="form-control">
                                    </div>
                                    <div class="form-group col-md-4">
                                        <label>所在地区</label>

                                        <input type="text" name="org_region" placeholder="所在地区" class="form-control">
                                    </div>
                                    <div class="form-group col-md-4">
                                        <label>所在地址</label>

                                        <input type="text" name="org_address" placeholder="所在地址" class="form-control">
                                    </div>

                                    <div class="form-group col-md-4">
                                        <label>联系人姓名</label>

                                        <input type="text" name="contacts_name" placeholder="联系人姓名" class="form-control">
                                    </div>

                                    <div class="form-group col-md-4">
                                        <label>联系人职务</label>

                                        <input type="text" name="contacts_duties" placeholder="联系人职务" class="form-control">
                                    </div>
                                    <div class="form-group col-md-4">
                                        <label>联系人电话</label>

                                        <input type="text" name="contacts_moblie" placeholder="联系人电话" class="form-control">
                                    </div>
                                    <div class="form-group col-md-4">
                                        <label>联系人性别</label>

                                        <select name="contacts_sex" class="form-control">
                                            <option value="1">先生</option>
                                            <option value="2">女士</option>
                                        </select>
                                    </div>

                                </div><!-- row -->
                                <label>机构简介</label>
                                <textarea name="org_desc" class="form-control" rows="5" placeholder="机构简介"></textarea>
                            </form>

                        </div><!-- panel-body -->

                        <div class="panel-footer" style="display: none;">
                            <button id="USER_ADD_FROM_BTN" class="btn btn-primary">添加</button>
                        </div><!-- panel-footer -->
                    </div><!-- panel -->

                </div>
            </div>

            <table id="exRowTable" class="table table-striped table-bordered">
                <thead>
                <tr>
                    <th>序号</th>
                    <th>机构名称</th>
                    <th>机构等级</th>
                    <th>创建时间</th>
                    <th>所在区域</th>
                    <th>地址</th>
                    <th>邮编</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
                </thead>
            </table>
        </div><!-- panel -->

    </div>

</div>
<!--madol 添加用户 hide-->
<div id="user_add_madol" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">&times;</button>
                <h4 class="modal-title">修改机构信息</h4>
            </div>
            <div class="modal-body">
                <form id="org_edit_ajaxForm" method="post" action="{:url('admin/org/orgupd')}" enctype="multipart/form-data">

                    <div class="row">
                        <div class="form-group col-md-4">
                            <label>机构名称</label>
                            <input type="text" name="org_name" placeholder="机构名称" class="form-control">
                            <input type="hidden" name="org_sn">
                        </div>
                        <div class="form-group col-md-4">
                            <label>邮编</label>
                            <input type="text" name="post_code" placeholder="邮编" class="form-control">
                        </div>
                        <div class="form-group col-md-4">
                            <label>创建时间</label>

                            <input type="text" name="create_date" disabled="disabled"  placeholder="创建时间" class="form-control">
                        </div>
                        <div class="form-group col-md-4">
                            <label>状态</label>

                            <select name="mark"  class="form-control">
                                <option value="1">正常</option>
                                <option value="0">禁用</option>
                            </select>
                        </div>


                        <div class="form-group col-md-4">
                            <label>所在地区</label>

                            <input type="text" name="org_region" placeholder="所在地区" class="form-control">
                        </div>

                        <div class="form-group col-md-4">
                            <label>所在地址</label>

                            <input type="text" name="org_address" placeholder="所在地址" class="form-control">
                        </div>



                        <div class="form-group col-md-6">
                            <label>联系人姓名</label>

                            <input type="text" name="contacts_name" placeholder="联系人姓名" class="form-control">
                        </div>

                        <div class="form-group col-md-6">
                            <label>联系人职务</label>

                            <input type="text" name="contacts_duties" placeholder="联系人职务" class="form-control">
                        </div>

                        <div class="form-group col-md-6">
                            <label>联系人电话</label>

                            <input type="text" name="contacts_moblie" placeholder="联系人电话" class="form-control">
                        </div>
                        <div class="form-group col-md-6">
                            <label>联系人性别</label>
                            <select name="contacts_sex" class="form-control">
                                <option value="1">先生</option>
                                <option value="2">女士</option>
                            </select>
                            <!--<input type="text" name="org_address" placeholder="联系人电话" class="form-control">-->
                        </div>


                    </div>
                    <label>机构简介</label>

                    <textarea name="org_desc" class="form-control" rows="5" placeholder="机构简介" ></textarea>
                </form>
                <div class="row" style="padding-right: 1rem;padding-top: 1rem" >
                    <button style="float: right;" id="ORG_EDIT_FROM_BTN" class="btn btn-primary">提交</button>
                </div>
            </div>
        </div>
    </div>
</div>
<style>
    .hides{
        display: none;
    }

</style>
<script>
    jQuery('#select-search-hide').select2({
        minimumResultsForSearch: -1
    });

    var exRowTable;
    $(function(){

        $("#USER_MADOL_ADD_BTN").click(function () {
            $("#user_add_madol").modal("show")
        })

     /*   $("#user_add_ajaxForm").find('[name = "org_name"]').blur(function () {
            $(this).css('border','1px solid red')
        })*/
        // 添加机构
        $("#USER_ADD_FROM_BTN").click(function () {

            var org_name = $("#user_add_ajaxForm").find('[name = "org_name"]').val()

            // 账号
            if(org_name == null || org_name == undefined || org_name == ''){
                $("#user_add_ajaxForm").find('[name = "org_name"]').focus()
                // $("#user_add_ajaxForm").find('[name = "org_name"]').css('border','1px solid red')
                return;
            }

            $("#user_add_ajaxForm").ajaxSubmit(function (data) {
                console.log(data)
                if(data == 0){
                    $(".hides").hide()
                    $("#succ_file_s").html("添加失败")
                    $("#faild_file_s").show()
                }else{
                    $(".hides").hide()
                    $("#succ_file_s").html("添加成功")
                    $("#succ_file_s").show()
                }
                exRowTable.ajax.reload();
            });


        })
        // 修改机构
        $("#ORG_EDIT_FROM_BTN").click(function () {

            var org_name = $("#org_edit_ajaxForm").find('[name = "org_name"]').val()

            // 账号
            if(org_name == null || org_name == undefined || org_name == ''){
                $("#org_edit_ajaxForm").find('[name = "org_name"]').focus()
                return;
            }

            $("#org_edit_ajaxForm").ajaxSubmit(function (data) {
                // console.log(data)
                if(data == 0){
                    $(".hides").hide()
                    $("#faild_file_s").html("修改失败")
                    $("#faild_file_s").show()
                }else{
                    $(".hides").hide()
                    $("#succ_file_s").html("修改成功")
                    $("#succ_file_s").show()
                }
                exRowTable.ajax.reload();
            });


        })

        $("[data-toggle='tooltip']").tooltip();




        exRowTable = $('#exRowTable').DataTable({
            responsive: true,
            "fnDrawCallback": function(oSettings) {
                jQuery('#exRowTable_paginate ul').addClass('pagination-active-success');
            },
            "ajax": "{:url('admin/org/orgquery')}",
            "columns": [
                { "data": "org_sn" },
                { "data": "org_name" },
                { "data": "org_grade" , "render": function ( data, type, full, meta ) {
                        switch(data){
                            case 1:
                                return '厂商';
                                break;
                            case 2:
                                return '代理商';
                            case 3:
                                return '健身房';
                                break;
                            default:
                                return '默认';

                                break;
                        }
                    }
                    },
                { "data": "create_date" },
                { "data": "org_region" },

                { "data": "org_address" },
                { "data": "post_code" },
                { "data": "mark", "render": function ( data, type, full, meta ) {
                        switch(data){
                            case '1':
                                return '正常';
                                break;
                            case '2':
                                return '禁用';
                                break;
                        }
                    }},
                { "data": "org_sn","render": function ( data, type, full, meta ) {
                        return '<button onclick="org_edit_view('+data+')" class="btn btn-primary btn-xs" >修改</button> <span>  </span><button onclick="org_del('+data+')" class="btn btn-danger btn-xs">删除</button>';
                    }},
            ],
            "order": [[0, 'desc']]
        });




    })

    function org_del(id) {
        if(confirm('确认删除！')){

            $.post("{:url('admin/org/orgdel')}",{ID:id},function (data) {

                if(data == 0){
                    $(".hides").hide()
                    $("#faild_file_s").html("修改失败")
                    $("#faild_file_s").show()
                }else if(data == 'error'){
                    $(".hides").hide()
                    $("#faild_file_s").html("该机构为管理机构，不能删除")
                    $("#faild_file_s").show()
                }else{
                    $(".hides").hide()
                    $("#succ_file_s").html("删除成功")
                    $("#succ_file_s").show()
                }
                exRowTable.ajax.reload();
            })
        }
    }
    
    function org_edit_view(id) {
        $.post("{:url('admin/org/orgsel')}",{ID:id},function (data) {
            var data = eval("("+data+")")
            // console.log(data)
            $("#org_edit_ajaxForm").find('[name = "org_name"]').val(data.org_name)
            $("#org_edit_ajaxForm").find('[name = "post_code"]').val(data.post_code)
            $("#org_edit_ajaxForm").find('[name = "org_sn"]').val(data.org_sn)
            $("#org_edit_ajaxForm").find('[name = "org_region"]').val(data.org_region)
            $("#org_edit_ajaxForm").find('[name = "org_desc"]').val(data.org_desc)
            $("#org_edit_ajaxForm").find('[name = "org_code"]').val(data.org_code)
            $("#org_edit_ajaxForm").find('[name = "org_address"]').val(data.org_address)
            $("#org_edit_ajaxForm").find('[name = "mark"]').val(data.mark)
            $("#org_edit_ajaxForm").find('[name = "create_date"]').val(data.create_date)

            $("#org_edit_ajaxForm").find('[name = "contacts_duties"]').val(data.contacts_duties)
            $("#org_edit_ajaxForm").find('[name = "contacts_moblie"]').val(data.contacts_moblie)
            $("#org_edit_ajaxForm").find('[name = "contacts_name"]').val(data.contacts_name)
            $("#org_edit_ajaxForm").find('[name = "contacts_sex"]').val(data.contacts_sex)
        })
        $("#user_add_madol").modal('show')
    }


</script>



